package com.kaili.tms.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.tms.pojo.dto.ShiftsRulesAddDTO;
import com.kaili.tms.pojo.dto.ShiftsRulesQueryDTO;
import com.kaili.tms.pojo.dto.ShiftsRulesUpdateDTO;
import com.kaili.tms.pojo.entity.ShiftsRulesEntity;
import com.kaili.tms.pojo.vo.RouteCodeOrNameVO;
import com.kaili.tms.pojo.vo.ShiftsRulesVO;
import com.kaili.tms.service.ShiftsRulesService;
import com.kaili.tms.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Desc: 班次维护
 * @author: zengshuai
 * @Time: 19-2-19 上午9:30
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Validated
@Api(tags = "班次维护", description = "班次维护相关API")
public class ShiftsRulesController {

    @Autowired
    private ShiftsRulesService shiftsRulesService;

    @GetMapping("/shiftsRules")
    @ApiOperation(value = "根据车线和编码规则查询班次信息")
    public Page<ShiftsRulesVO> queryShiftsRules(@ModelAttribute ShiftsRulesQueryDTO queryDTO){
        Page<ShiftsRulesVO> pager = new Page<>(queryDTO.getPage(),queryDTO.getPageSize());
        return shiftsRulesService.queryShiftsRules(pager,queryDTO);
    }

    @PostMapping("/shiftsRules")
    @ApiOperation(value = "新增班次")
    @Transactional
    public RestStatus addShiftsRules(@RequestBody ShiftsRulesAddDTO addDTO){
        ShiftsRulesEntity entity = new ShiftsRulesEntity();
        BeanUtils.copyProperties(addDTO,entity);
        Integer createdId = LoginDetailUtils.getCurrentEmployeeId();
        if(createdId==null){
            entity.setCreatedBy(-1);
        }else {
            entity.setCreatedBy(createdId);
        }
        entity.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        entity.setSiteId(LoginDetailUtils.getSiteId().longValue());
        Integer count = shiftsRulesService.addShiftsRules(entity);
        if(count == 1){
            return CommonStatus.ADD_OK;
        } else {
            return CommonStatus.ADD_FAIL;
        }
    }

    @PutMapping("/shiftsRules")
    @ApiOperation(value = "修改班次")
    public RestStatus updateShiftsRules(@RequestBody ShiftsRulesUpdateDTO updateDTO){
        ShiftsRulesEntity entity = new ShiftsRulesEntity();
        BeanUtils.copyProperties(updateDTO,entity);
        Integer modifyId = LoginDetailUtils.getCurrentEmployeeId();
        if(entity.getId() != null){
            if(modifyId==null){
                entity.setModifyBy(-1);
            }else {
                entity.setModifyBy(modifyId);
            }
        }
        entity.setModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Integer count = shiftsRulesService.updateShiftsRules(entity);
        if(count == 1){
            return CommonStatus.UPDATE_OK;
        } else {
            return CommonStatus.UPDATE_FAIL;
        }
    }

    @DeleteMapping("/shiftsRules/{id}")
    @ApiOperation(value = "删除班次")
    public RestStatus deleteShiftsRulesById(@PathVariable Long id){
        Integer count = shiftsRulesService.deleteShiftsRules(id);
        if(count == 1){
            return CommonStatus.DELETE_OK;
        } else {
            return CommonStatus.DELETE_FAIL;
        }
    }

    @GetMapping("/trainNum")
    @ApiOperation(value = "获取编码规则")
    public String getTrainNum(){
        return shiftsRulesService.getTrainNum();
    }

    @GetMapping("/carRoute/{code}")
    @ApiOperation(value = "车线编码或名称")
    public List<RouteCodeOrNameVO> getRouteCodeOrName(@PathVariable("code") String code){
        return shiftsRulesService.getRouteCodeOrName(code);
    }

}
